Requires Scripting PRO
The BluetoothPeripheralManager API enables your device to act as a Bluetooth Low Energy (BLE) peripheral. It allows you to:
This API is ideal for building custom sensor devices, BLE peripheral simulators, controllers, and similar use cases.
isAdvertising: Promise<boolean>Returns whether the device is currently advertising as a peripheral.
startAdvertising(advertisementData: { localName?: string; serviceUUIDs?: string[] }): Promise<void>Begins BLE advertising with optional device name and service UUIDs.
stopAdvertising(): Promise<void>Stops ongoing BLE advertising.
addService(service): Promise<void>Adds a service and its characteristics to the peripheral.
Parameters:
Example:
removeService(serviceUUID: string): Promise<void>Removes a previously added service by its UUID.
removeAllServices(): Promise<void>Removes all services added by the current script.
onRestoreState: ((state) => void) | nullCalled when the system restores your script due to a background BLE session. Useful for restoring service and advertising state.
onReadyToUpdateSubscribers: (() => void) | nullCalled when the system's transmission queue is cleared and ready to send notifications again after a previous failure due to queue congestion.
onReadCharacteristicValue: (characteristicId, offset, central) => Promise<{result, value}>Invoked when a remote central requests to read a characteristic.
If not implemented, the system returns readNotPermitted.
Signature:
onWriteCharacteristicValue: (characteristicId, offset, value, central) => Promise<BluetoothATTResponseCode>Invoked when a remote central attempts to write to a characteristic.
If not implemented, the system returns writeNotPermitted.
Signature:
onSubscribe: (characteristicId, central) => voidCalled when a central subscribes to a characteristic that supports notifications or indications.
onUnsubscribe: (characteristicId, central) => voidCalled when a central unsubscribes from a characteristic.
getSubscribers(characteristicId: string): Promise<Central[]>Returns a list of central devices currently subscribed to a given characteristic.
Each item:
updateValue(characteristicId: string, value: Data, options?): Promise<boolean>Sends a notification or indication to all subscribed centrals (or a specified subset) with the updated characteristic value.
Returns:
true: Successfully sentfalse: Queue is full — wait for onReadyToUpdateSubscribers before retryingsetDesiredConnectionLatency(centralId: string, latency): Promise<void>Sets the preferred connection latency for a specific central device.
"low" – Faster interaction, higher power usage"medium" – Balanced"high" – Lower power usage, less frequent interactionBluetoothATTResponseCode EnumerationDefines response codes for read/write operations:
| Name | Value | Meaning |
|---|---|---|
success |
0 | Operation succeeded |
invalidHandle |
1 | Invalid handle |
readNotPermitted |
2 | Read not permitted |
writeNotPermitted |
3 | Write not permitted |
invalidPdu |
4 | Invalid PDU |
insufficientAuthentication |
5 | Not authenticated |
requestNotSupported |
6 | Request not supported |
invalidOffset |
7 | Invalid offset |
insufficientAuthorization |
8 | Not authorized |
prepareQueueFull |
9 | Prepare queue is full |
attributeNotFound |
10 | Attribute not found |
attributeNotLong |
11 | Attribute not long |
insufficientEncryptionKeySize |
12 | Encryption key size too small |
invalidAttributeValueLength |
13 | Invalid attribute value length |
unlikelyError |
14 | Unlikely error occurred |
insufficientEncryption |
15 | Encryption required |
unsupportedGroupType |
16 | Unsupported group type |
insufficientResources |
17 | Insufficient resources |